package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.EnMkt;
import com.cfets.annualAssessment.mapper.EnMktMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/4/11.
 */
@Service
public class EnMktService {
    @Autowired
    private EnMktMapper enMktMapper;

    public boolean trunEnMkt(){
        Example example=new Example(EnMkt.class);
        return enMktMapper.deleteByExample(example)>0;
    }

    public List<EnMkt> getEnMktPage(EnMkt enMkt) {
        Page<?> page = PageHelper.startPage(enMkt.getPage(), enMkt.getRows());
        List<EnMkt> enMkts = this.getEnMkt(enMkt);
        enMkt.setTotalCount(page.getTotal());
        return enMkts;
    }

    public List<EnMkt> getEnMktsIn7(EnMkt enMkt) {
        //七天内的随意
        Example example=new Example(EnMkt.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(enMkt.getOrg_name())){
            criteria.andLike("org_name","%"+enMkt.getOrg_name()+"%");
        }
        criteria.andIsNotNull("enter_time");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - 7);
        date=c.getTime();
        String str=sdf.format(date);
        criteria.andGreaterThanOrEqualTo("enter_time",str);
        return enMktMapper.selectByExample(example);
    }

    public List<EnMkt> getEnMktsOut7(EnMkt enMkt) {
        //七天前
        Example example=new Example(EnMkt.class);
        Example.Criteria criteria =example.createCriteria();
        criteria.andNotEqualTo("enter_stat","受理完成，系统管理员账号已发送邮箱");
        if(!StringUtils.isEmpty(enMkt.getOrg_name())){
            criteria.andLike("org_name","%"+enMkt.getOrg_name()+"%");
        }
        criteria.andIsNotNull("enter_time");
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date=new Date();
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, - 7);
        date=c.getTime();
        String str=sdf.format(date);
        criteria.andLessThan("enter_time",str);
        return enMktMapper.selectByExample(example);
    }
    public List<EnMkt> getEnMkt(EnMkt enMkt) {
        Example example=new Example(EnMkt.class);
        Example.Criteria criteria =example.createCriteria();
//        if(!StringUtils.isEmpty(enMkt.getEnter_stat())){
//            criteria.andEqualTo("enter_stat",enMkt.getEnter_stat());
//        }
        if(!StringUtils.isEmpty(enMkt.getEnter_stat())){
            criteria.andLike("enter_stat","%"+enMkt.getEnter_stat()+"%");
        }
        if(!StringUtils.isEmpty(enMkt.getOrg_name())){
            criteria.andLike("org_name","%"+enMkt.getOrg_name()+"%");
        }
        return enMktMapper.selectByExample(example);
    }

    public boolean newEnMkt(EnMkt enMkt) {
        return enMktMapper.insertSelective(enMkt)>0;
    }

    public boolean updateEnMkt(EnMkt enMkt) {
        return enMktMapper.updateByPrimaryKey(enMkt)>0;
    }

    public boolean deleteEnMktById(int id) {
        return enMktMapper.deleteByPrimaryKey(id)>0;
    }
}
